package com.aurora.app.interceptor;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;


import com.aurora.app.constant.Conf;
import com.aurora.app.service.WeixinService;



public class JsapiTicketInterceptor extends HandlerInterceptorAdapter {

	private final Logger LOG = LoggerFactory.getLogger(this.getClass());
	
	@Autowired
	private WeixinService weixinService;

	/**
	 * 不包含的路径
	 */
	private List<String> excludeUrls;

	/**
	 * @return the excludeUrls
	 */
	public List<String> getExcludeUrls() {
		return this.excludeUrls;
	}

	/**
	 * @param excludeUrls
	 *            the excludeUrls to set
	 */
	public void setExcludeUrls(List<String> excludeUrls) {
		this.excludeUrls = excludeUrls;
	}

	@Override
	public boolean preHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler) throws Exception {
		String requestPath = request.getRequestURI();
		String queryString = request.getQueryString();
		if (StringUtils.isNotBlank(queryString)) {
			requestPath += "?" + queryString;
		}
		requestPath = requestPath.substring(request.getContextPath().length());// 去掉项目路径
		
		LOG.debug("请求网页"+requestPath);
		for (String excludeUrl : this.excludeUrls) {
			if (requestPath.startsWith(excludeUrl)) {
				return true;
			}
		}
		String method = request.getMethod();
		if (method.equals("GET")) {
			try {
				this.weixinService.getSign(request, Conf.USED2_ID);
			} catch (Exception e) {
				LOG.error("获取签名失败", e);
			}
		}
		return true;

	}
}
